<template>
  <view class="container">
    <u-navbar
      :is-back="true"
      :background="{ background: '#fff' }"
      :border-bottom="false"
      id="nav-bar"
      title="物料商城"
    >
      <view slot="backIcon" class="cartBox">
        <image
          class="cartIcon"
          src="/static/images/icon-cart.png"
          @click="navigateTo('/pages/purchase/cart')"
        />
        <text class="num">{{ count || 0 }}</text>
      </view>
    </u-navbar>

    <u-tabs
      :list="tabList"
      :is-scroll="false"
      :current="tabCurrent"
      @change="change"
    ></u-tabs>
    <!-- <mescroll-body
      ref="mescrollRef"
      @init="mescrollInit"
      top="0"
      @down="downCallback"
      :up="upOption"
      @up="upCallback"
    > -->
    <view class="list">
      <view v-for="(item, index) in list" :key="index" class="item" @click='handleDetail(item)'>
        <view class="item-in">
          <image class="item-img" :src="item.imgUrl" mode="aspectFill" />
          <view class="item-name">{{ item.name }}</view>
          <view class="item-remarks">{{ item.remarks || ' ' }}</view>
          <view class="item-bottom">
            <view class="item-title">
              {{ item.integral * item.minNum }}
              <text class="fen">/台</text></view
            >
            <image
              class="item-btn"
              mode="aspectFill"
              src="/static/images/icon-cartImg.png"
              @click="buyClick(item)"
            />
          </view>
        </view>
      </view>
    </view>
    <!-- </mescroll-body> -->
  </view>
</template>

<script>
import util from '@/common/utils.js';
import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins.js';

export default {
  mixins: [MescrollMixin],
  data() {
    return {
      upOption: {
        page: {
          num: 0,
          size: 10000,
        },
        noMoreSize: 5,
        empty: {
          tip: '',
        },
      },
      mescroll: null,
      tabList: [
        {
          name: '全部',
          count: 0,
        },
        {
          name: '汇付天下',
          count: 0,
        },
        {
          name: '联动优势',
          count: 0,
        },
        {
          name: '拉卡拉',
          count: 0,
        },
      ],
      tabCurrent: 0,
      list: [
        {
          id: 0,
          name: '积分商城',
          imgUrl:
            'https://img14.360buyimg.com/n1/s450x450_jfs/t1/246297/2/16373/35951/66c8a072Fc02c9f02/b2a6cefc921fcefd.jpg.avif',
          minNum: 1,
          integral: 100,
          type: '汇付天下',
        },
        {
          id: 1,
          name: '积分商城',
          imgUrl:
            'https://img11.360buyimg.com/n1/jfs/t1/219016/5/45348/58267/67101f0bF920a32a0/37863718eccc1fdb.jpg',
          minNum: 1,
          integral: 10,
        },
      ],
      count: 0,
      cartList: [],
    };
  },
  onLoad(options) {
    this.getData();
  },
  methods: {
    navigateTo(url) {
      uni.navigateTo({
        url: url,
      });
    },
	getData() {},
    mescrollInit(mescroll) {
      this.mescroll = mescroll;
    },
    downCallback() {
      this.list = [];
      this.mescroll.resetUpScroll();
    },
    upCallback(page) {
      this.getIntegralProduct(page);
    },
    getIntegralProduct(page) {
      // const that = this;
      // const params = {
      //   pageNumber: page.num,
      //   pageSize: page.size,
      // };
      // service.getIntegralProduct(
      //   params,
      //   function (res) {
      //     if (res && res.data) {
      //       that.mescroll.endSuccess(res.data.length);
      //       if (page.num == 1) that.list = [];
      //       that.list = that.list.concat(res.data);
      //     }
      //   },
      //   function () {}
      // );
    },
    change(index) {
      this.tabCurrent = index;
      this.list = [];
      this.mescroll.resetUpScroll();
    },
    buyClick(item) {
      uni.$u.throttle(this.buyIt(item), 1000);
    },
    buyIt(item) {
      const newObject = {
        productId: item.id,
        num: item.minNum,
        name: item.name,
        imgUrl: item.imgUrl,
        minNum: item.minNum,
        integral: item.integral,
        type: item.type,
      };
      let index = this.cartList.findIndex(
        (val) => val.productId === newObject.productId
      );
      var cartList = this.cartList;

      if (
        this.cartList.findIndex(
          (val) => val.productId === newObject.productId
        ) !== -1
      ) {
        let proObj = {
          productId: item.id,
          num: this.cartList[index].num + 1,
          name: item.name,
          imgUrl: item.imgUrl,
          minNum: item.minNum,
          integral: item.integral,
          type: item.type,
        };
        cartList.splice(index, 1, proObj);
      } else {
        cartList.push(newObject);
      }

      let totalCount = cartList.reduce((pre, cur) => {
        return pre + cur.num;
      }, 0);
      this.count = totalCount;
      this.cartList = cartList;
      util.showToast('添加成功');
    },
    gotoCart() {
      if (this.cartList.length === 0) {
        util.showToast('请先添加商品到购物车');
        return;
      }

      uni.navigateTo({
        url: '/pages/exchange/cart?cartList=' + JSON.stringify(this.cartList),
      });
    },
    handleDetail(data) {
      uni.navigateTo({
        url: '/pages/purchase/goodsDetail',
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.container {
  position: relative;
  width: 100%;
  min-height: 100%;
  padding: 0;
  background: #f5f5f5;
  box-sizing: border-box;
}

.list {
  width: 100%;
  padding: 0 15rpx 160rpx;
  background-color: #f5f5f5;
  overflow: hidden;
}

.item {
  float: left;
  width: 50%;
  padding: 30rpx 15rpx 0;
  box-sizing: border-box;
}

.item-in {
  padding: 0 0 30rpx;
  background: #ffffff;
  border-radius: 10rpx;
  .item-img {
    width: 100%;
    height: 320rpx;
  }
  .item-name {
    margin-top: 20rpx;
    width: 100%;
    padding: 0 20rpx;
    font-size: 26rpx;
    font-weight: bold;
    color: #333333;
    line-height: 1;
  }
  .item-remarks {
    margin-top: 12rpx;
    width: 100%;
    padding: 0 20rpx;
    font-size: 24rpx;
    font-weight: 400;
    color: #999999;
  }
}

.item-bottom {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  .item-title {
    flex: 1;
    padding-left: 20rpx;
    line-height: 1;
    font-size: 24rpx;
    font-weight: 400;
    color: #f53131;
    box-sizing: border-box;
    .fen {
      font-size: 18rpx;
      font-weight: 400;
      color: #999999;
    }
  }
  .item-btn {
    margin-right: 20rpx;
    width: 36rpx;
    height: 36rpx;
  }
}

.cartBox {
  position: relative;
  width: 48rpx;
  height: 48rpx;
  .cartIcon {
    width: 48rpx;
    height: 48rpx;
  }
  .num {
    position: absolute;
    top: -10rpx;
    right: -10rpx;
    display: inline;
    padding: 0 4rpx;
    height: 30rpx;
    font-family: PingFang SC, sans-serif;
    font-weight: 500;
    font-size: 18rpx;
    color: #ffffff;
    line-height: 30rpx;
    background: #f73736;
    border-radius: 12rpx;
  }
}
</style>
